import * as echarts from '../../ec-canvas/echarts';
import * as util from '../../utils/util';
var chefChart = null;
var cid = null
var option = null
var times = 0
function initChart(start, end, chefIdList){
  let data = null
  // let option = {}
  if(start == end && times > 0){
    chefChart.setOption(option)
    return
  }
  let postData = { 
    "chefIdList": chefIdList, 
    "dateRange": [start, end] 
  }
  getApp().baseByData('client/company/' + cid + '/chef/detail', postData, function (res) {
    times += 1
    data = res.data.data
    option = {
      color: ['#003366', '#006699', '#4cabce'],
      legend: {
        data: ['销售量', '销售额', '毛利']
      },
      calculable: true,
      xAxis: [
        {
          type: 'category',
          data: data.chiefName,
          axisLabel: {
            interval: 0,
            rotate: 40
          }
        }
      ],
      yAxis: [
        {
          type: 'value',
          name: '元',
          axisLabel: {
            formatter: '￥{value}'
          }
        },
        {
          type: 'value',
          name: '销售量',
          axisLabel: {
            formatter: '{value}'
          }
        }
      ],
      series: [
        {
          name: '销售量',
          type: 'bar',
          barGap: 0,
          data: data.saleCount
        },
        {
          name: '销售额',
          type: 'bar',
          data: data.saleAmount
        },
        {
          name: '毛利',
          type: 'bar',
          data: data.gross
        }
      ]      
    }
    // console.log(optionData)
    chefChart.setOption(option)
  },function(res){
    if(res.data.status != 200){
      wx.showToast({
        title: res.data.message,
        icon: 'none',
        duration: 1500
      })
    }else{
      wx.showToast({
        title: '暂无数据',
        icon: 'none',
        duration: 1500
      })
    }
  }) 
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chefList: [{ name: '全部' }],
    index: 0,
    start: util.formatTime(new Date()),
    end: util.formatTime(new Date()),
    showModal:false,
    chefTable:[],
    detailTable: [],
    dateValidate: false,
    chef: null,
    chefD: {
      disableTouch: true,
      onInit: function (canvas, width, height) {
        chefChart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(chefChart);
        initChart(util.formatTime(new Date()), util.formatTime(new Date()),[])
        return chefChart;
      }
    }
  },
  onReady: function(){
    this.getChef()
  },
  // 点击显示详情蒙版
  toggleModal: function(e){
    let _self = this
    if (!e.target.dataset.chefId){
      _self.setData({
        showModal: !this.data.showModal
      })
      // chefChart.setOption(option)
      return
    }
    let postData = {
      chefId: e.target.dataset.chefId,
      dateRange: [_self.data.start, _self.data.end],
    }
    _self.getDetail(postData)
    // console.log(postData)
  },
  getDetail: function (postData){
    wx.showLoading()
    let _self = this
    getApp().baseByData('client/company/' + cid + '/chef/singlechefsale', postData, function (res) {
      // console.log(res.data)
      _self.setData({
        detailTable: res.data.data,
        showModal: !_self.data.showModal
      })
      wx.hideLoading()
    },function(res){
      if (res.data.status != 200) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 1500
        })
      } else {
        wx.showToast({
          title: '暂无数据',
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
  getChef: function(){
    wx.showLoading()
    let _self = this
    getApp().baseInfo('client/company/'+getApp().globalData.companyId+'/cook/list',function(res){
      res.data.data.unshift({ name: '全部' })
      wx.hideLoading()
      // console.log(res.data)
      _self.setData({
        chefList: res.data.data,
        chef: res.data.data[0]
      })
      _self.getTable()
    },function(){
      wx.hideLoading()
      _self.getTable()
    })
  },
  getTable: function(){
    wx.showLoading()
    let _self = this
    let chefIdList = []
    if (_self.data.chef.id){
      chefIdList = [_self.data.chef.id]
    }    
    let data = { 
      'companyId': getApp().globalData.companyId, 
      'chefIdList': chefIdList, 
      'dateRange': [_self.data.start, _self.data.end]  
      // 'dateRange': ['2018-09-25', '2018-09-26']      
    }
    getApp().requestByData('food/cook/sale/detail',data,function(res){
      // console.log(res.data)
      wx.hideLoading()
      _self.setData({
        chefTable: res.data.data
      })
      if(!res.data.data || res.data.data.length == 0){
        wx.showToast({
          title: '暂无数据',
          icon: 'none'
        })
      }
    })
  },
  search: function(){
    let _self = this
    times = 0
    if (!_self.data.chef.id){
      _self.getTable()
      initChart(_self.data.start, _self.data.end, [])
    } else {
      _self.getTable()
      initChart(_self.data.start, _self.data.end, [_self.data.chef.id])
    }
    
  },
  //-- end
  chefChange: function (e) {
    this.setData({
      chef: this.data.chefList[e.detail.value]
    })    
  },
  check: function () {
    let start = this.data.start
    let end = this.data.end
    this.setData({
      dateValidate: !(start <= end)
    })
  },
  bindDateChangeEnd: function (e) {
    // console.log(e.detail.value)
    this.setData({
      end: e.detail.value
    })
    this.check()
  },
  bindDateChangeStart: function (e) {
    // console.log(e.detail.value)
    this.setData({
      start: e.detail.value
    })
    this.check()
  },
    /**
   * 顶部样式
   */
  onLoad: function (options) {
    cid = getApp().globalData.companyId
    wx.setNavigationBarTitle({ title: '厨师绩效' });
  }
})